package com.situ.day7.cal;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class CalFactory {

    public static AbstractCal createCal() {
        AbstractCal cal = null;
        try {
            //1.通过当前的类获取类加载器
            ClassLoader classLoader = CalFactory.class.getClassLoader();
            //2.通过类加载器的方法获取输入流
            InputStream inputStream = classLoader.getResourceAsStream("cal.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            String className = properties.getProperty("className");
            Class clazz = Class.forName(className);
            cal = (AbstractCal) clazz.newInstance();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return cal;
    }

    public static AbstractCal createCal1(int calType) {
        AbstractCal cal = null;
        switch (calType) {
            case 1:
                cal = new Add();
                break;
            case 2:
                cal = new Sub();
                break;
            case 3:
                cal = new Div();
                break;
        }
        return cal;
    }

    public static AbstractCal createCal2(String calType) {
        AbstractCal cal = null;
        switch (calType) {
            case "add":
                cal = new Add();
                break;
            case "sub":
                cal = new Sub();
                break;
            case "div":
                cal = new Div();
                break;
        }
        return cal;
    }
}
